<template>
    <el-form :inline="true" :model="formInline" :rules="rules" class="demo-form-inline" ref="form">
        <el-form-item label="账号" prop="username">
            <el-input v-model="formInline.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="密码" prop="password" required="boolean" validateMessage="sb">
            <el-input v-model="formInline.password" placeholder="请输入密码" clearable />
        </el-form-item>
        <el-form-item label="请再次输入密码" prop="repassword" required="boolean" validateMessage="dawdawdaw">
            <el-input v-model="formInline.repassword" placeholder="请再次输入密码" clearable />
        </el-form-item>
        <el-button type="primary" @click="registerdata">注册</el-button>
    </el-form>
</template>
  
<style></style>
<script lang="ts" setup>
import { ref } from 'vue'
import { instanceRegister } from '../api/modules/register'
import { useRouter } from 'vue-router'
const input = ref('')
const formInline = ref({
    username: '',
    password: '',
    repassword: ''
})
// 声明规则
const rules = {
    username: [{ required: true, message: '请输入', trigger: 'blur' }],
    password: [{ pattern: /^[a-zA-Z0-9]{6,}$/, message: '请输入正确的密码', trigger: 'blur' }],
    repassword: [{ pattern: /^[a-zA-Z0-9]{6,}$/, message: '请输入正确的密码', trigger: 'blur' },
    {
        validator: (rule, value, callback) => {
            if (value !== formInline.value.password) {
                callback(new Error('俩次输入的值不一样'))
            } else {
                callback()
            }
        }
    }

    ]
}
const Router = useRouter()
const form = ref()
const registerdata = async () => {
    await form.value.validate()
    console.log('注册成功');
    await instanceRegister(formInline.value).then((res) => {
        console.log(res);
        alert(res.data.message)
        if (res.data.message = '注册成功') {
            setInterval(() => {
                Router.push('/login')
            }, 2000)
        }else{
            return 
        }

    }).catch((err) => {
        console.log(err);

    })


}

</script>